
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

import com.batchaps.global.GameEngine;
import com.batchaps.menu.GameMenu;
import com.batchaps.menu.MenuManager;


public class CanvasMain extends GameCanvas implements Runnable{
	
	Main midlet;
	MenuManager menuManager;
	Image imgbackground;
	
	public CanvasMain(final Main midlet){
		super(true);
		setFullScreenMode(true);
		this.midlet = midlet;
		
		//constructor
		Init();
	}
	
	public void Init()
	{
		//init menumanager
		menuManager = new MenuManager(GameEngine.k_Screen_Width, GameEngine.k_Screen_Height);
		//int bacground
		try {
			imgbackground = Image.createImage("/background.jpg");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//
		Thread t =new Thread(this);
		t.start();
	}

	public void run() {
		// TODO Auto-generated method stub
		while(true)
		{
			menuManager.Update();
			
			Draw();
			
			try {
	            Thread.sleep(20);
	        } catch (InterruptedException ex) {
	            ex.printStackTrace();
	        }
		}
	}
	
	protected void pointerReleased(int arg0, int arg1) {
		// TODO Auto-generated method stub
		menuManager.IsClick(arg0, arg1);
	}
	
	protected void pointerPressed(int arg0, int arg1) {
		// TODO Auto-generated method stub
		menuManager.pointerPress(arg0, arg1);
	}
	
	public void Draw()
    {
		Graphics g = getGraphics();
		
		Image temp = Image.createImage(GameEngine.k_Screen_Width, GameEngine.k_Screen_Height);
    	Graphics gtemp = temp.getGraphics();
    	gtemp.drawImage(imgbackground, 0 , 0 , 0);
    	menuManager.Draw(gtemp);
    	
    	
    	g.drawImage(temp, 0 , 0, 0);
    	flushGraphics();
    }

}
